{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github"
      },
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/gaussian-splatting-colab/blob/main/gaussian_splatting_viewer_colab.ipynb)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "!git clone https://github.com/camenduru/splat\n",
        "!wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -O /content/cloudflared-linux-amd64.deb\n",
        "!dpkg -i cloudflared-linux-amd64.deb\n",
        "\n",
        "import atexit, requests, subprocess, time, re, os\n",
        "from random import randint\n",
        "from threading import Timer\n",
        "from queue import Queue\n",
        "def cloudflared(port, metrics_port, output_queue):\n",
        "    atexit.register(lambda p: p.terminate(), subprocess.Popen(['cloudflared', 'tunnel', '--url', f'http://127.0.0.1:{port}', '--metrics', f'127.0.0.1:{metrics_port}'], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT))\n",
        "    attempts, tunnel_url = 0, None\n",
        "    while attempts < 10 and not tunnel_url:\n",
        "        attempts += 1\n",
        "        time.sleep(3)\n",
        "        try:\n",
        "            tunnel_url = re.search(\"(?P<url>https?:\\/\\/[^\\s]+.trycloudflare.com)\", requests.get(f'http://127.0.0.1:{metrics_port}/metrics').text).group(\"url\")\n",
        "        except:\n",
        "            pass\n",
        "    if not tunnel_url:\n",
        "        raise Exception(\"Can't connect to Cloudflare Edge\")\n",
        "    output_queue.put(tunnel_url)\n",
        "output_queue, metrics_port = Queue(), randint(8100, 9000)\n",
        "thread = Timer(2, cloudflared, args=(7860, metrics_port, output_queue))\n",
        "thread.start()\n",
        "thread.join()\n",
        "tunnel_url = output_queue.get()\n",
        "os.environ['webui_url'] = tunnel_url\n",
        "print(tunnel_url)\n",
        "%cd /content/splat\n",
        "!python -m http.server 7860"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
